﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CurrencyTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name", 1, "format")]
        public void Can_CRUD(string code,
            string name, 
            decimal rate, 
            string priceFormat)
        {
            var currency = Processor.Execute(new CreateCurrency
            {
                Code = code,
                Name = name,
                Rate = rate,
                PriceFormat = priceFormat
            }).Result;

            Assert.NotNull(currency);
            currency.Code.AreEqualIgnoringCase(code);
            currency.Name.AreEqualIgnoringCase(name);
            currency.Rate.AreEqual(rate);
            currency.PriceFormat.AreEqualIgnoringCase(priceFormat);
            currency.AreEqual(Processor.Execute(new GetCurrency
            {
                EntityID = currency.EntityID
            }).Result);
            currency.AreEqual(Processor.Execute(new GetCurrencyByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            name = name.Reverse();
            rate = rate + 1;
            priceFormat = priceFormat.Reverse();
            var upd_currency = Processor.Execute(new UpdateCurrency
            {
                EntityID = currency.EntityID,
                Code = code,
                Name = name,
                Rate  = rate,
                PriceFormat = priceFormat,
                Flags = CurrencyFlags.Active
            }).Result;

            Assert.NotNull(upd_currency);
            upd_currency.AreEqual(currency);
            upd_currency.Code.AreEqualIgnoringCase(code);
            upd_currency.Name.AreEqualIgnoringCase(name);
            upd_currency.Rate.AreEqual(rate);
            upd_currency.PriceFormat.AreEqualIgnoringCase(priceFormat);
            upd_currency.Flags.AreEqual(CurrencyFlags.Active);

            Processor.Execute(new DeleteCurrency
            {
                EntityID = currency.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCurrency
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateCurrency
            {
                Code = String.Empty
            }));
        }

        [TestCase(0)]
        [TestCase(-1)]
        public void Cannot_Create_If_Rate_EqOrLt_Zero(decimal rate)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCurrency
            {
                Code = "xy",
                Rate = rate
            }));
        }

        [TestCase(0)]
        [TestCase(-1)]
        public void Cannot_Update_If_Rate_EqOrLt_Zero(decimal rate)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateCurrency
            {
                Code = "xy",
                Rate = rate
            }));
        }
    }
}
